//
//  MusicCell.m
//  KidRobot
//
//  Created by Mac on 15/12/25.
//  Copyright © 2015年 QBB. All rights reserved.
//

#import "MusicCell.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"

@interface MusicCell ()


@property (weak, nonatomic) IBOutlet UILabel *musicGroupName;
@property (weak, nonatomic) IBOutlet UIButton *MoreMusicButton;
@property (weak, nonatomic) IBOutlet UIImageView *musicImageFirst;
@property (weak, nonatomic) IBOutlet UILabel *musicLabelFirst;
@property (weak, nonatomic) IBOutlet UIImageView *musicImageSecend;
@property (weak, nonatomic) IBOutlet UILabel *musicLabelSecend;
@property (weak, nonatomic) IBOutlet UIImageView *musicImageThire;
@property (weak, nonatomic) IBOutlet UILabel *musicLabelThire;
@property (weak, nonatomic) IBOutlet UIImageView *musicImageFour;
@property (weak, nonatomic) IBOutlet UILabel *musicLabelFour;
@property (weak, nonatomic) IBOutlet UIImageView *groupicon;

@end

@implementation MusicCell
{
    NSDictionary *model1;
    NSDictionary *model2;
    NSDictionary *model3;
    NSDictionary *model4;
}
-(void)setHighlighted:(BOOL)highlighted{
    
}

-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    
}

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.layer.shouldRasterize = YES;
        //栅格化必须要制定分辨率
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        // 2.异步绘制 如果cell比较复杂,可以使用
        self.layer.drawsAsynchronously = YES;
    }
    return self;
}

- (IBAction)MoreMusicClick:(id)sender {
    
    NSLog(@"MoreMusicClick");
    
    UIButton * button = (UIButton*)sender;
    
    NSLog(@"%lu",button.tag);
    
    //    [self.delegate MoreMusic:self.dataDict[@"title"] andMusicType:[self.dataDict[@"id"] integerValue]];
    
    //    DTTypeModel * model = [[DTTypeModel alloc]init];
    //
    //    model.DT_id = self.DTModel.DT_moduleId;
    //    model.DT_title = self.DTModel.DT_moduleName;
    
    NSDictionary * dict = @{
                            };
    
    [self.delegate MoreMusicTo:dict atIndex:button.tag];
}

- (void)awakeFromNib {
    self.layer.shouldRasterize = YES;
    //栅格化必须要制定分辨率
    self.layer.rasterizationScale = [UIScreen mainScreen].scale;
    // 2.异步绘制 如果cell比较复杂,可以使用
    self.layer.drawsAsynchronously = YES;
    [super awakeFromNib];
    
    CGFloat width = 0.0;
    CGFloat rowW = 0.0;
    if ([UIScreen mainScreen].bounds.size.width < 380) {
        width = PXChange(75);
        rowW = PXChange(15);
    }
    else{
        width = PXChange(82);
        rowW = PXChange(20);
    }
    
    
    [self updateConstraintsIfNeeded];
    //    [self.musicGroupName mas_updateConstraints:^(MASConstraintMaker *make) {
    //        make.left.equalTo(self.contentView).offset(PX_TO_W(15));
    //    }];
    
    
    self.musicImageFirst.layer.masksToBounds = YES;
    self.musicImageFirst.layer.borderWidth = 0.5;
    self.musicImageFirst.layer.borderColor = [UIColor colorWithHexString:@"d3d3d7"].CGColor;
    self.musicImageFirst.layer.cornerRadius = 15;
    
    //    [self.musicImageFirst mas_updateConstraints:^(MASConstraintMaker *make) {
    //
    //        make.size.mas_equalTo(CGSizeMake(width,width));
    //        make.left.equalTo(self.contentView).offset(rowW);
    //    }];
    
    UIControl * button1 = [[UIControl alloc]initWithFrame:self.musicImageFirst.frame];
    
    [button1 addTarget:self action:@selector(CloseClick1) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:button1];
    
    [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.musicImageFirst);
        
    }];
    
    
    
    self.musicImageSecend.layer.masksToBounds = YES;
    self.musicImageSecend.layer.borderWidth = 0.5;
    self.musicImageSecend.layer.borderColor = [UIColor colorWithHexString:@"e3e3e3"].CGColor;
    self.musicImageSecend.layer.cornerRadius = 15;
    
    //    [self.musicImageSecend mas_updateConstraints:^(MASConstraintMaker *make) {
    //
    //        make.size.mas_equalTo(CGSizeMake(width,width));
    //        make.left.equalTo(self.musicImageFirst.mas_right).offset(rowW);
    //    }];
    
    UIControl * button2 = [[UIControl alloc]initWithFrame:self.musicImageFirst.frame];
    
    [button2 addTarget:self action:@selector(CloseClick2) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:button2];
    
    [button2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.musicImageSecend);
        
    }];
    
    
    self.musicImageThire.layer.masksToBounds = YES;
    self.musicImageThire.layer.cornerRadius = 15;
    self.musicImageThire.layer.borderWidth = 0.5;
    self.musicImageThire.layer.borderColor = [UIColor colorWithHexString:@"e3e3e3"].CGColor;
    
    //    [self.musicImageThire mas_updateConstraints:^(MASConstraintMaker *make) {
    //
    //        make.size.mas_equalTo(CGSizeMake(width,width));
    //        make.left.equalTo(self.musicImageSecend.mas_right).offset(rowW);
    //    }];
    
    UIControl * button3 = [[UIControl alloc]initWithFrame:self.musicImageFirst.frame];
    [button3 addTarget:self action:@selector(CloseClick3) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button3];
    [button3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.musicImageThire);
    }];
    self.musicImageFour.layer.masksToBounds = YES;
    self.musicImageFour.layer.cornerRadius = 15;
    self.musicImageFour.layer.borderWidth = 0.5;
    self.musicImageFour.layer.borderColor = [UIColor colorWithHexString:@"e3e3e3"].CGColor;
    
    UIControl * button4 = [[UIControl alloc]initWithFrame:self.musicImageFour.frame];
    
    [button4 addTarget:self action:@selector(CloseClick4) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:button4];
    
    [button4 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.musicImageFour);
        
    }];
}

- (void)loadCell:(NSIndexPath*)index
{
    
    self.MoreMusicButton.tag = index.row;
    self.musicGroupName.text = [self.dataDic objectForKey:@"des"];
    NSString *valueStr = [self.dataDic objectForKey:@"desjson"];
    NSDictionary *dic = [NBATools dictionaryWithJsonString:valueStr];
    NSMutableArray *albumArr = dic[@"data"];
    model1 = albumArr[0];
    model2 = albumArr[1];
    model3 = albumArr[2];
    model4 = albumArr[3];
    [self.musicImageFirst sd_setImageWithURL:[NSURL URLWithString:[model1 objectForKey:@"covimage"]] placeholderImage:[UIImage imageNamed:@"jpghold"]];
    
    self.musicLabelFirst.text = [model1 objectForKey:@"title"];
    [self.musicImageSecend sd_setImageWithURL:[NSURL URLWithString:[model2 objectForKey:@"covimage"]] placeholderImage:[UIImage imageNamed:@"jpghold"]];
    
    self.musicLabelSecend.text = [model2 objectForKey:@"title"];
    
    
    
    [self.musicImageThire sd_setImageWithURL:[NSURL URLWithString:[model3 objectForKey:@"covimage"]] placeholderImage:[UIImage imageNamed:@"jpghold"]];
    
    self.musicLabelThire.text = [model3 objectForKey:@"title"];
    
    [self.musicImageFour sd_setImageWithURL:[NSURL URLWithString:[model4 objectForKey:@"covimage"]] placeholderImage:[UIImage imageNamed:@"jpghold"]];
    
    self.musicLabelFour.text = [model4 objectForKey:@"title"];
    
}
- (void)LoadWithDic:(NSDictionary *)dic withIndex:(NSInteger)index{
    self.MoreMusicButton.tag = index;
    self.musicGroupName.text = [dic objectForKey:@"name"];
    [self.groupicon sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"ico"]] placeholderImage:[UIImage imageNamed:@"分类_英语启蒙"]];
    NSMutableArray *albumArr = [dic objectForKey:@"res"];
    NSDictionary *model1 = albumArr[0];
    NSDictionary *model2 = albumArr[1];
    NSDictionary *model3 = albumArr[2];
    //      DTGroupModel *model4 = albumArr[3];
    
    
    [self.musicImageFirst sd_setImageWithURL:[NSURL URLWithString:[model1 objectForKey:@"picture"]] placeholderImage:[UIImage imageNamed:@"music_default_icon"]];
    
    self.musicLabelFirst.text = [model1 objectForKey:@"name"];
    
    
    [self.musicImageSecend sd_setImageWithURL:[NSURL URLWithString:[model2 objectForKey:@"picture"]] placeholderImage:[UIImage imageNamed:@"music_default_icon"]];
    
    self.musicLabelSecend.text = [model2 objectForKey:@"name"];
    
    
    
    [self.musicImageThire sd_setImageWithURL:[NSURL URLWithString:[model3 objectForKey:@"picture"]] placeholderImage:[UIImage imageNamed:@"music_default_icon"]];
    
    self.musicLabelThire.text = [model3 objectForKey:@"name"];
}

-(void)CloseClick1{
    [self.delegate musicToGroupFromDic:model1];
    
}

-(void)CloseClick2{
    [self.delegate musicToGroupFromDic:model2];
    
}

-(void)CloseClick3{
    [self.delegate musicToGroupFromDic:model3];
    
}

-(void)CloseClick4{
    [self.delegate musicToGroupFromDic:model4];
}



- (IBAction)playToRobot:(id)sender {
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(musicCell:withPlaySong:withIndexPath:)])
    {
        [self.delegate musicCell:self withPlaySong:nil withIndexPath:self.indexPath];
    }
}


@end
